var prmoseHttp = require('../../utils/prmoseHttp.js');
var util = require('../../utils/util')
var app = getApp();
Component({
  mixins: [],
  data: {
    toDaytui:'',
    toAdtui:'',
    dhAdvertGuide:'',
    dhAwardDialog:'',
    imgHead:'https://rhi0.xiyun.net/zhuandian/image/',
    dhAdvInfor:{},
    percent:0,
    bannerInfor:{},
  },
  props: {
    isTop:{
      type:Boolean,
      value:false
    },
    isGoTb:{
      type:Boolean,
      value:false
    },
    isHideGo:{
      type:Boolean,
      value:false
    },
  },
  didMount() {
    let that = this;
    my.getSystemInfo({
      success: (res) => {
        that.setData({
          'statusBarHeight': res.statusBarHeight,//状态栏高度 
          'titleBarHeight': res.titleBarHeight,//标题栏高度
          'navHeight': res.statusBarHeight + res.titleBarHeight, //导航栏高度
          'imgHeight': res.windowWidth * 0.86, //背景图片高度
          'toDaytui': res.windowWidth / 698 * 96 + 35,
          'toAdtui': res.windowWidth / 716 * 171,
        })
      }
    });
    
  },
  didUpdate() {
    
  },
  didUnmount() {
    
  },
  methods: {
    //灯火广告每日可完成数，已完成数
  getDhInfo(){
    let that = this;
    prmoseHttp.publicRequest(prmoseHttp.apiAddress.queryDh,{},1,'get').then(res=>{
      if(res.code == 200){
        that.setData({
          dhAdvInfor:res.data
        })
        //发送灯火广告信息 给首页
        that.props.onDhInfo(res.data);
        let percent = 0
        if(res.data.finishedNumber > 0){
          percent = parseInt((res.data.finishedNumber/res.data.totalNumber)*100)
        }else{
          percent = 0;
        }
        that.setData({
          percent:percent
        })
      }
    }).catch(err=>{})
  },
    goAd(){
      this.setData({
        isGoTb:true,
        type:1,
      })
      let startTime = Date.parse(new Date())/1000;
      my.setStorage({key:"dhBrowseTime",data:startTime})
    },
    onGoInfor(){
      this.props.onGoInfor();
      // this.setData({
      //   isGoTb:true,
      //   type:2,
      // })
      // let startTime = Date.parse(new Date())/1000;
      // my.setStorage({key:"dhBrowseTime",data:startTime})
      // my.setStorage({key:"dhNum",data:1})
    },
    //引导页隐藏显示
    getTipsShow(){
      let that = this;
      let date = new Date();
      let nowTime = date.getFullYear() +'-'+ date.getMonth() +'-'+ date.getDate();
      if(my.getStorageSync({key:'nowTime'}).data){
        let ago = my.getStorageSync({key:'nowTime'}).data;
        if(nowTime != ago){
          my.removeStorageSync({key:'nowTime'});
          my.setStorage({key:'nowTime',data:nowTime});
          that.getTipsStatus();
        }else{
          that.setData({
            dhAdvertGuide:true,
            dhAwardDialog:true,
          })
          that.props.onStatus(true,true);
        }
      }else{
        my.setStorage({key:'nowTime',data:nowTime});
        that.setData({
          dhAdvertGuide:true,
          dhAwardDialog:true,
        })
        that.props.onStatus(true,true);
        // that.getTipsStatus();
      }
    },
    //查询后台灯火广告引导页开关状态
    getTipsStatus(){
      let that = this;
      prmoseHttp.publicRequest(prmoseHttp.apiAddress.querySwtich,{},1,'get').then(res=>{
        if(res.code == 200){
          that.setData({
            dhAdvertGuide:res.data.dhAdvertGuide,
            dhAwardDialog:res.data.dhAwardDialog,
          })
        }
      })
    },
    //获取灯火banner广告信息
  getBannerInfo(){
    let that = this;
    prmoseHttp.publicRequest(prmoseHttp.apiAddress.queryBanner,{},1,'get').then(res=>{
      if(res.code == 200){
        that.setData({
          bannerInfor:res.data
        })
        //发送banner信息给首页
        that.props.onBannerInfo(res.data)
      }
    }).catch(err=>{})
  },
  getDh(){
    let that = this;
    // console.log(app.globalData.rsa)
      let time = util.formDate();
      my.rsa({
        action: 'encrypt',
        text: my.getStorageSync({key:'userInfo'}).data.thirdPartyUserId,
        // 设置公钥，需替换你自己的公钥
        key:app.globalData.rsa,
        success: result => {
          prmoseHttp.dhRequest(prmoseHttp.apiAddress.dhYindao,{
            uid:result.text,
            timestamp:time,
          }).then(res=>{
            if (res.code == 10000 && res.result == true) {
              // 开启引导
              // console.log('设置引导');
              that.setData({ 
                dhAdvertGuide: true,
                dhAwardDialog: true
              });
            }else if(that.data.dhAdvertGuide == true){
              that.setData({ 
                dhAdvertGuide: false,
                dhAwardDialog: false
              });
            }
          })
        },
        fail(e) {
          my.alert({
            content: e.errorMessage || e.error,
          });
        },
      });
    // }
  },
  },
});
